<?php

/**
 * 
 */
class Admin_Form_Usuario extends Zend_Form
{
	/**
	 * (non-PHPdoc)
	 * @see Zend_Form::init()
	 */
	public function init ()
	{
		//Atributos
		$this->setMethod( Zend_Form::METHOD_POST );
		
		$this->setAttrib( 'class', 'form' );
		$this->setAttrib( 'id', 'form-usuario' );
		$this->setAttrib( 'onsubmit', 'return save(this);' );
		
		//Elementos
		$elements = array();
		
		$decorator = array( 'ViewHelper' );
		
		$elements[] = $this->createElement('hidden', 'user_id')
			->setDecorators($decorator);
			
		$elements[] = $this->createElement('text', 'user_nome')
			->addFilter('StripTags')
			->addFilter('StringTrim') 
			->setAttrib('class', 'full-width')
			->setAttrib('maxlength', 80)
			->setDecorators($decorator)
			->setRequired(true);
			
		$elements[] = $this->createElement('text', 'user_email')
			->addValidator('EmailAddress')
			->addFilter('StripTags')
			->addFilter('StringTrim')
			->setAttrib('maxlength', 50)
			->setAttrib('class', 'full-width')
			->setDecorators($decorator)
			->setRequired(true);
			
		$passwordConfirmation = new App_Validate_PasswordConfirm();

		$elements[] = $this->createElement('password', 'user_senha')
			->setLabel('Senha')
			->addValidator('StringLength', false, array(6, 20))
			->addValidator($passwordConfirmation)
			->setAttrib('class', 'full-width')
			->setAttrib('maxlength', 50)
			->setDecorators($decorator)
			->setRequired(true);

        $elements[] = $this->createElement('password', 'confirma_senha')
			->setDecorators(array('ViewHelper'))
			->setLabel('Confirma senha')
			->setAttrib('maxlength', 50)
			->setAttrib('class', 'full-width')
			->setRequired(true);
			
		$elements[] = $this->createElement('checkbox', 'user_status')
			->setAttrib('class', 'mini-switch')
			->setValue(1)
			->setDecorators($decorator);
		
		$this->addElements( $elements );
	}
	
	/**
     *
     * @param array $data
     * @return bool 
     */
    public function isValid( $data )
    {
        if ( !empty( $data['user_id'] ) ) {

            $this->getElement('user_senha')->setRequired( false );
            $this->getElement('confirma_senha')->setRequired( false );
        }

        return parent::isValid( $data );
    }
}